package com.edcarlos.servico.adapter;

import java.util.List;
import java.util.zip.Inflater;

import com.edcarlos.servico.bean.R.id;
import com.edcarlos.servico.entity.TipoServico;

import android.R.layout;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class TipoServicoAdapter extends BaseAdapter {

	private List<TipoServico> listaTipo;
	private Context context;
	private LayoutInflater inflater;
	
	public TipoServicoAdapter(Context context,List<TipoServico> lista) {
		this.context = context;
		this.listaTipo = lista;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public void notifyDataSetChanged() {
		try{
            super.notifyDataSetChanged();
        }catch (Exception e) {
            trace("Erro : " + e.getMessage());
        }
		
	}
	
	public void remover(final TipoServico tipo){
		this.listaTipo.remove(tipo);
	}
	
	public void adicionar(final TipoServico tipo){
		this.listaTipo.add(tipo);
	}
	@Override
	public int getCount() {
		return listaTipo.size();
	}

	@Override
	public Object getItem(int position) {
		return listaTipo.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TipoServico tipo = listaTipo.get(position);
		ViewHolder holder;
		try{
		if(convertView == null){
			
			convertView = inflater.inflate(layout.activity_list_item, null);
			
			holder = new ViewHolder();
			holder.txtvDescricao = (TextView) convertView.findViewById(id.txtvDecricao);
			holder.txtvSetor = (TextView) convertView.findViewById(id.txtvSetor);
			holder.txtvInterno = (TextView) convertView.findViewById(id.txtvInterno);;
			
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.txtvDescricao.setText(tipo.getDescricao());
		holder.txtvSetor.setText(tipo.getSetor().getId());
		holder.txtvInterno.setText(tipo.getTipoInternoExterno().getCodigo());
		
		return convertView;
		
		}catch(Exception e){
			trace(e.getMessage());
		}
		return convertView;
	}

	public void toast (String msg)
    {
        Toast.makeText (context, msg, Toast.LENGTH_SHORT).show ();
    } 
     
    private void trace (String msg) 
    {
        toast (msg);
    } 
    
    static class ViewHolder {
        public TextView txtvDescricao;
        public TextView txtvSetor;
        public TextView txtvInterno;
    }   
     
}
